package org.agent.aigccore.dao.impl;

import jakarta.annotation.Resource;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.agent.aigccore.basic.enums.GeneralConfigStatusEnum;
import org.agent.aigccore.dao.GeneralConfigRepository;
import org.agent.aigccore.dao.mappers.mysql.GeneralConfigMapper;
import org.agent.aigccore.model.bo.GeneralConfigBO;
import org.agent.aigccore.model.po.GeneralConfigPO;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static org.agent.aigccore.model.bo.GeneralConfigBO.convert;

@Slf4j
@Setter
@Component
public class GeneralConfigRepositoryImpl implements GeneralConfigRepository {

    @Resource
    private GeneralConfigMapper generalConfigMapper;

    @Override
    public GeneralConfigBO load(Long id, boolean lock) {
        if (lock) {
            return convert.formPO(generalConfigMapper.lock(id));
        }
        return convert.formPO(generalConfigMapper.load(id));
    }

    @Override
    public GeneralConfigBO loadUK(String domain, String scene, String ckey) {
        return convert.formPO(generalConfigMapper.loadUK(domain, scene, ckey));
    }

    @Override
    public List<GeneralConfigBO> page(GeneralConfigStatusEnum status, String domain, String scene, int offset, int pageSize) {
        List<GeneralConfigPO> pos = generalConfigMapper.page(
                status != null ? status.getCode() : null,
                domain, scene,
                offset, pageSize);
        log.info("[GeneralConfigRepository.page] 召回数量={}", pos.size());
        return pos.stream().map(po -> convert.formPO(po)).collect(Collectors.toList());
    }

    @Override
    public int insert(GeneralConfigBO generalConfigBO) {
        GeneralConfigPO po = convert.toPO(generalConfigBO);
        int num = generalConfigMapper.insert(po);
        generalConfigBO.setId(po.getId());
        log.info("[GeneralConfigRepository.insert] 插入成功！num={}", num);
        return num;
    }

    @Override
    public int update(GeneralConfigBO generalConfigBO) {
        GeneralConfigPO po = convert.toPO(generalConfigBO);
        int num = generalConfigMapper.update(po);
        log.info("[GeneralConfigRepository.update] 更新成功！num={}", num);
        return num;
    }

    @Override
    public int delete(Long id) {
        int num = generalConfigMapper.delete(id);
        log.info("[GeneralConfigRepository.delete] 删除成功！num={}", num);
        return num;
    }
}